package com.yocard.yxyd.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.webkit.ValueCallback;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.umeng.analytics.MobclickAgent;
import com.yocard.yxyd.R;
import com.yocard.yxyd.adapter.InviteRecordsAdapter;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.http.model.request.OrderListRequest;
import com.yocard.yxyd.http.model.response.InviteRecord;
import com.yocard.yxyd.others.ClassicsHeader;
import com.yocard.yxyd.ui.contract.InviteRecordListContract;
import com.yocard.yxyd.ui.fragment.base.BaseFragment;
import com.yocard.yxyd.ui.presenter.InviteRecordListPresenter;
import com.yocard.yxyd.utils.util.SizeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created on 2019/5/28
 * 邀请记录
 */
public class InviteRecordListFragment extends BaseFragment<InviteRecordListPresenter> implements InviteRecordListContract.View {

    @BindView(R.id.empty_view)
    RelativeLayout emptyView;
    @BindView(R.id.recycle_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.refresh_header)
    ClassicsHeader classicsHeader;
    @BindView(R.id.tv_tip)
    TextView tvTip;

    protected String csmId;
    private ValueCallback<Integer> callback;

    private String status; //区分已挖成(finished)和未完成

    private int currentPage = 1;
    List<InviteRecord.DataBean> datas = new ArrayList<>();
    protected InviteRecordsAdapter mAdapter;
    private OrderListRequest orderListRequest;

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.ft_invite_record_list;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        classicsHeader.setCallback(callback);
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    protected void initEventAndData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            csmId = bundle.getString("csmId");
//            type = bundle.getString("type");
        }
        mAdapter = new InviteRecordsAdapter(datas);
        mAdapter.setType(status);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter);

        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(ContextCompat.getDrawable(getActivity(),R.drawable.shape_transparent));
        recyclerView.addItemDecoration(dividerItemDecoration);

        refreshLayout.setOnRefreshListener(refreshLayout1 -> {
            currentPage = 1;
            mPresenter.fetchOrderList(currentPage, status);
        });

        refreshLayout.setOnLoadMoreListener(refreshLayout1 -> {
            currentPage++;
            mPresenter.fetchOrderList(currentPage, status);
        });

        showLoadingDialog();
        mPresenter.fetchOrderList(currentPage, status);

//        mAdapter.setOnItemClickListener(value -> {
//            Bundle bundle = new Bundle();
//            bundle.putSerializable("data",value);
//            ActivityUtils.startActivity(bundle,OrderDetailActivity.class);
//        });
    }

    public void setRefreshCallbacl(ValueCallback<Integer> callback) {
        this.callback = callback;
        if(classicsHeader != null){
            classicsHeader.setCallback(callback);
        }
    }

    @Override
    public void OnfetchOrderList(List<InviteRecord.DataBean> list) {
        if (currentPage == 1) {
            datas.clear();
            if (list.size() == 0) {
                tvTip.setText(getString(R.string.empty_invite_list));

                showEmptyView(emptyView);
            } else {
                hiddenEmptyView(emptyView);
                datas.addAll(list);
            }
            refreshLayout.finishRefresh();
//            refreshLayout.resetNoMoreData();
            mAdapter.notifyDataSetChanged();
            dismisDialog();
        } else {
            datas.addAll(list);
            refreshLayout.finishLoadMore();
        }
        if (list.size() < Constants.PAGE_SIZE) {
            refreshLayout.finishLoadMoreWithNoMoreData();
        }
        mAdapter.notifyDataSetChanged();
    }

    protected void showEmptyView(View view) {
        view.setVisibility(View.VISIBLE);
    }

    @Override
    public void showErrorMsg(String msg) {
        super.showErrorMsg(msg);
        dismisDialog();
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
//        if(datas.size() > 0) {
//            return;
//        }
        //清楚列表数据
        datas.clear();
        mAdapter.notifyDataSetChanged();
//        if(currentPage == 1){
//            showEmptyView(emptyView);
//            tvTip.setText(getString(R.string.empty_order_list));
//        }
        showEmptyView(emptyView);
        tvTip.setText(getString(R.string.empty_order_list));
    }

    public void scrollTop(){
        recyclerView.smoothScrollToPosition(0);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public void refresh(){//提供给父fragment刷新
        recyclerView.scrollToPosition(0);
        refreshLayout.autoRefresh();
    }
}
